import request from '@/utils/request'

export interface Question {
  id: number
  title: string
  content: string
  type: 'SINGLE_CHOICE' | 'MULTIPLE_CHOICE' | 'TRUE_FALSE' | 'FILL_BLANK' | 'ESSAY'
  difficulty: number
  score: number
  subjectId: number
  subjectName: string
  categoryId?: number
  categoryName?: string
  options?: Array<{
    content: string
    correct: boolean
  }>
  answer?: string
  explanation?: string
  createdAt: string
  updatedAt: string
}

export interface QuestionQuery {
  page?: number
  size?: number
  query?: string
  type?: string
  difficulty?: number
  subjectId?: number
  categoryId?: number
}

export interface PageResponse<T> {
  content: T[]
  totalElements: number
  totalPages: number
  number: number
  size: number
}

export const questionApi = {
  /**
   * 获取试题列表
   */
  getQuestions: (params: QuestionQuery) =>
    request.get<PageResponse<Question>>('/questions', { params }),

  /**
   * 获取试题详情
   */
  getQuestion: (id: number) =>
    request.get<Question>(`/questions/${id}`),

  /**
   * 创建试题
   */
  createQuestion: (data: Omit<Question, 'id' | 'createdAt' | 'updatedAt' | 'subjectName' | 'categoryName'>) =>
    request.post<Question>('/questions', data),

  /**
   * 更新试题
   */
  updateQuestion: (id: number, data: Partial<Question>) =>
    request.put<Question>(`/questions/${id}`, data),

  /**
   * 删除试题
   */
  deleteQuestion: (id: number) =>
    request.delete(`/questions/${id}`),

  /**
   * 导出试题
   */
  exportQuestions: (params: QuestionQuery) =>
    request.get('/questions/export', { 
      params,
      responseType: 'blob'
    })
}